package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.service.FunctionServer;
import com.campus.commons.entity.EveningSubject;
import com.campus.app.mapper.EveningSubjectMapper;
import com.campus.app.service.EveningSubjectsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;

import com.campus.commons.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 晚自习排课表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class EveningSubjectServiceImpl extends ServiceImpl<EveningSubjectMapper, EveningSubject> implements EveningSubjectsService {
    @Autowired
    private EveningSubjectMapper eveningSubjectMapper;
    @Autowired
    private FunctionServer functionServer;

    @Override
    public Result<List<EveningSubject>> getSubjects(int current, int size, Date start, Date end, String className) {
        String star = Tool.getDateToString(start);
        String ends = Tool.getDateToString(end);
        //endregion

        Page<EveningSubject> page = new Page<>(current, size);
        QueryWrapper<EveningSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql(className!=null,"classId","(select c.id from class c where c.className like '%"+className+"%')");
        queryWrapper.between(start!=null,"Date(date)", star, ends);
         eveningSubjectMapper.selectPage(page, queryWrapper);
        return Result.result(page);
    }

    @Override
    public Result<List<EveningSubject>> getNowSubjects() {
        LambdaQueryWrapper<EveningSubject> queryWrapper = new QueryWrapper<EveningSubject>().lambda();
        queryWrapper.eq(EveningSubject::getDate, Tool.getDateToString(null));
        return Result.result(eveningSubjectMapper.selectList(queryWrapper));
    }

    @Override
    public Result<List<EveningSubject>> getAllSubjects(int current, int size) {
        return Result.result(eveningSubjectMapper.selectPage(new Page<EveningSubject>(current, size), null));
    }

    @Override
    public Result addSubject(@NotNull EveningSubject eveningSubject) {
//
        return Result.result(eveningSubjectMapper.insert(eveningSubject));
    }

    @Override
    public Result delSubject(@NotNull @Min(1) Integer subjectId) {
        int res = eveningSubjectMapper.deleteById(subjectId);
        if (res == 1) {
            return Result.success();
        } else {
            return Result.error(Error.System, null);
        }
    }

    @Override
    public Result upSubject(@NotNull EveningSubject eveningSubject) {
        if (eveningSubject.getId() <= 0) {
            return Result.error(Error.PARAMS_E3R, "参数Id不正确");
        }
//        验证；
        int i = eveningSubjectMapper.updateById(eveningSubject);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error(Error.System, null);
        }
    }

    @Override
    public Result uploadSubject(InputStream fileStream, String fileName) {
        try {
            List<EveningSubject> subjects = functionServer.upLoadEveningSubject(fileStream, fileName);
            //插入；
            int i = eveningSubjectMapper.addSubjects(subjects);
            return Result.result(subjects.size(), i);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(Error.File_E2R, null);
        }
    }
}
